package com.xtool.livewallpaper

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.ViewGroup.MarginLayoutParams
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES
import androidx.core.view.updateLayoutParams
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.xtool.livewallpaper.base.BaseActivity
import com.xtool.livewallpaper.base.UIHelper
import com.xtool.livewallpaper.base.dp
import com.xtool.livewallpaper.databinding.ActivitySplashBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@SuppressLint("CustomSplashScreen")
class SplashActivity : BaseActivity() {
    private val binding: ActivitySplashBinding by lazy {
        ActivitySplashBinding.inflate(
            layoutInflater
        )
    }

    override fun onStart() {
        super.onStart()
        val uiMode = resources.configuration.uiMode
        val isNightMode = AppCompatDelegate.getDefaultNightMode() == MODE_NIGHT_YES
                || uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
        ImmersionBar.with(this)
            .transparentStatusBar()
            .statusBarDarkFont(!isNightMode)
            .init()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        animate()
        launch {
            delay(1000)
            startActivity(Intent(this@SplashActivity, MainActivity::class.java))
            finish()
        }
    }

    private fun animate() {
        val translationYAnimator = ObjectAnimator.ofFloat(
            binding.tvWelcome,
            "translationY",
            binding.tvWelcome.translationY,
            0f
        )

        val alphaAnimator =
            ObjectAnimator.ofFloat(binding.tvWelcome, "alpha", binding.tvWelcome.alpha, 1f)

        val animatorSet = AnimatorSet()
        animatorSet.playTogether(translationYAnimator, alphaAnimator)
        animatorSet.duration = 800
        animatorSet.start()
    }
}